/*
 * Copyright (C) 2017 grandcentrix GmbH
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.grandcentrix.thirtyinch.sample;

import net.grandcentrix.thirtyinch.TiPresenter;
import net.grandcentrix.thirtyinch.rx.RxTiPresenterSubscriptionHandler;
import net.grandcentrix.thirtyinch.rx.RxTiPresenterUtils;
import rx.Observable;
import rx.Subscription;
import rx.schedulers.Schedulers;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;

import java.util.concurrent.TimeUnit;

class HelloWorldPresenter extends TiPresenter<HelloWorldView> {

    private int counter = 0;
    private BehaviorSubject<String> textSubject = BehaviorSubject.create();
    private RxTiPresenterSubscriptionHandler rxSubscriptionHelper = new RxTiPresenterSubscriptionHandler(this);
    private PublishSubject<Void> triggerHeavyCalculation = PublishSubject.create();

    @Override
    protected void onCreate() {
        super.onCreate();

        textSubject.onNext("Hello World!");

        rxSubscriptionHelper.manageSubscription(Observable.interval(0, 1, TimeUnit.SECONDS)
                .compose(RxTiPresenterUtils.deliverLatestToView(this))
                .subscribe(uptime -> sendToView(view -> view.showPresenterUpTime(uptime))));

        rxSubscriptionHelper.manageSubscription(triggerHeavyCalculation
                .onBackpressureDrop((n) -> textSubject.onNext("Don't hurry me!"))
                .doOnNext((n) -> textSubject.onNext("calculating next number..."))
                .flatMap((n) -> increaseCounter(), 1)
                .doOnNext((n) -> textSubject.onNext("Count: " + counter))
                .subscribe());
    }

    @Override
    protected void onAttachView(HelloWorldView view) {
        super.onAttachView(view);

        Subscription showTextSub = textSubject.asObservable()
                .subscribe((it) -> {
                    view.showText(it);
                });
        Subscription onButtonClickSub = view.onButtonClicked()
                .subscribe((n) -> triggerHeavyCalculation.onNext(null));

        rxSubscriptionHelper.manageViewSubscriptions(showTextSub, onButtonClickSub);
    }

    /**
     * fake a heavy calculation
     */
    private Observable<Integer> increaseCounter() {
        return Observable.just(counter)
                .subscribeOn(Schedulers.computation())
                // fake heavy calculation
                .delay(2, TimeUnit.SECONDS)
                .doOnNext((n) -> {
                    counter++;
                    textSubject.onNext("value: " + counter);
                });
    }
}
